#include <conio.h>
#include <graphics.h>
#pragma comment(lib,"Winmm.lib")

#define high 600  // 游戏画面尺寸
#define width 350

// 全局变量
IMAGE img_bk, img_bird1, img_bird2, img_barup1, img_barup2, img_bardown1, img_bardown2;
int bird_x, bird_y;//小鸟位置
int barup_x, barup_y;//上面缺口
int bardown_x, bardown_y;//下面缺口
int highth=160;//通道高度
int score;//分数
int pace=2;//加速度
void startup()  // 数据初始化
{
    //播放背景音乐
	mciSendString("open E:\\background.mp3 alias bkmusic",NULL,0,NULL);
    mciSendString("play bkmusic repeat",NULL,0,NULL);


	initgraph(width,high);//画面大小
	
	//导入图片
	loadimage(&img_bk, "E:\\background.jpg");
	
	loadimage(&img_bird1, "E:\\bird1.jpg");
	loadimage(&img_bird2, "E:\\bird2.jpg");
	
	loadimage(&img_barup1, "E:\\bar_up1.gif");
	loadimage(&img_barup2, "E:\\bar_up2.gif");
    
	loadimage(&img_bardown1, "E:\\bar_down1.gif");
	loadimage(&img_bardown2, "E:\\bar_down2.gif");
	
	//设定初始坐标
    bird_x=100;
	bird_y=200;
	barup_x = 300;
	barup_y = -200-(rand()%2+1)*100;
	bardown_x = 300;
	bardown_y=600+barup_y+highth;

	score=0;

	BeginBatchDraw();
}

void clean()  // 显示画面
{
	

}

void show()  // 显示画面
{
    putimage(0, 0, &img_bk);
	
	putimage(bird_x, bird_y,&img_bird1,NOTSRCERASE);
	putimage(bird_x, bird_y, &img_bird2,SRCINVERT);
	
	putimage(barup_x, barup_y, &img_barup1, NOTSRCERASE);
	putimage(barup_x, barup_y, &img_barup2, NOTSRCERASE);
	
	putimage(bardown_x, bardown_y, &img_bardown1, NOTSRCERASE);
	putimage(bardown_x, bardown_y, &img_bardown2, NOTSRCERASE);
	
	FlushBatchDraw();
	outtextxy(300,100,score);
	// 延时
	Sleep(3);
}

void updateWithoutInput()  // 与用户输入无关的更新
{
	static int speed=5; 
	if(speed<5)
	    speed++;
	if(speed==5)
	{
		//小鸟下落
    	if(bird_y<580)
	    	bird_y=bird_y+pace;
		speed=0;

		static int p=15; 
	    if(p<15)
	        p++;
	    if(p==15)
		{
		    if(pace<15)
			    pace++;
			p=0;
		}
		//柱子迁移
		if(barup_x>-141)
		{
	    	barup_x-=1;
	    	bardown_x-=1;
		}
	}

	//柱子重新出现
	if(barup_x+140==0)
	{
	    barup_x=350;
	    bardown_x=350;

		barup_y=-200-(rand()%2+1)*100;
		bardown_y=600+barup_y+highth;
	}
   
	//磕脑袋（
	if(bird_y<=0||bird_y+20>=580)
	{
        mciSendString("close bkmusic ",NULL,0,NULL);
		exit(0);
	}

	//撞柱子
	if((bird_x+35>barup_x)&&(bird_x<barup_x+140))
	{
		if((bird_y+14>bardown_y)||(bird_y-14<=barup_y+600))
		{
			mciSendString("close bkmusic ",NULL,0,NULL);
		    exit(0);
		}
	}

	//得分
	if(bird_x==barup_x+140)
		score++;
    
}

void updateWithInput()  // 与用户输入有关的更新
{
	char input;
	//上升
	if(kbhit())
	{
		input=getch();
		if(input==' ')
		{  
			//音效
			mciSendString("close jpmusic ",NULL,0,NULL);
			mciSendString("open E:\\Jump.mp3 alias jpmusic ",NULL,0,NULL);
			mciSendString("play jpmusic ",NULL,0,NULL);
			
			bird_y=bird_y-60;
			
			pace=2;
		}

	}
}

void gameover()
{
	EndBatchDraw();
	closegraph();
}

int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		clean();  // 把之前绘制的内容清除
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
		show();  // 显示新画面
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}